package xupt.com.bolg.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xupt.com.bolg.entity.Comn;
import xupt.com.bolg.repository.ComnRepository;
import xupt.com.bolg.service.ColmunService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ComnServiceImpl implements ColmunService {
    @Autowired
    ComnRepository comnRepository;
    @Override
    public boolean save(Comn comn) {
        comn.setData(new Date());
        comnRepository.save(comn);
        return true;
    }

    @Override
    public boolean deleteByID(Integer id) {
        comnRepository.deleteById(id);
        return true;
    }

    @Override
    public Comn findByID(Integer id) {
        Optional<Comn> byId = comnRepository.findById(id);
        return byId.get();
    }

    @Override
    public List<Comn> findcomnByBolgID(Integer id) {
        List<Comn> all = comnRepository.findAll();
        List<Comn> list=new ArrayList<>();
        for(int i=0;i<all.size();i++){
            if(all.get(i).getBolgid()==id){
                list.add(all.get(i));
            }
        }
        return list;
    }
}
